<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use yii\web\Request;
use yii\web\Response;
use proxy\HttpProxy;
use helper\SessionHelper;
use PHPExcel;
use PHPExcel_Writer_Excel2007;

class ExcelController extends SecBaseController
{
    public function actionCall(){
        $response = Yii::$app->response;
        $response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $thisPath = $request->getPathInfo();
        $path_array = explode("/", $thisPath);//0:ajax, 1:call, 2,3:后端服务url 
        $thisRequestParams = $request->getQueryParams();
        $thisBodyParams = $request->getBodyParams();

        $excel_name = $request->get('excel_name') ; 
        if(!$excel_name){
           $excel_name = 'resume'; 
        }

        $req_params =  array_merge($this->remote_params, $thisRequestParams, $thisBodyParams);
        $new_params =  array();
        foreach ($req_params as $key => $value) {
            if(is_array($value)){
                foreach ($value as $k => $v) {
                    if(isset($new_params[$key])){
                        $new_params[$key] = $new_params[$key].','.$v;
                    }else{
                        $new_params[$key] = $v;
                    }
                    
                }
            }else{
                $new_params[$key] = $value;
            }
        }
        $new_params['page_size'] = 10000;
        $new_params['page_code'] = 1;
        try{ 
            $ret = $this->proxy->execute($path_array[2], $path_array[3],$new_params);
        }catch(\Exception $e){
            // $this->return_page_values['errno'] = $e->getCode();
            // $this->return_page_values['errmsg'] = $e->getMessage();
            // return $this->myRenderFile('@tpl/page/error.tpl', $this->return_page_values);
             return ['errno' => $e->getCode(), 'errmsg' => $e->getMessage()];
        }

        $objPHPExcel = new PHPExcel();

        $objPHPExcel->getProperties()->setCreator("admin_id:".$this->session->get('admin_user_id'));//创建人

        $objPHPExcel->getProperties()->setLastModifiedBy("admin_id:".$this->session->get('admin_user_id'));//最后修改人
        
        // $objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");//标题
        
        // $objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");//题目
        
        // $objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");//描述
        
        // $objPHPExcel->getProperties()->setKeywords("office 2007 openxml php");//关键字
        
        // $objPHPExcel->getProperties()->setCategory("Test result file");//种类


        
        $objPHPExcel->setActiveSheetIndex(0);//设置当前的sheet
        $objPHPExcel->getActiveSheet()->setTitle($excel_name);//设置sheet的name


        $letter = array('A','B','C','D','E','F','G','H','I','J','K',
            'L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF'); 
        // var_dump($ret);exit; 
        // var_dump($ret['column_title']); exit ;

       


        $column_title = $ret['column_title'] ;
        $data_list = $ret['data_list'] ;
        $i = 0 ; 
        foreach($column_title as $key => $value){
            // echo $value['name'].'<br>' ;
            if($value['intable']==1){
                $objPHPExcel->getActiveSheet()->setCellValue($letter[$i].'1',$value['name']);
                $i++;
            }
        }
        
        if(count($data_list)>0){
            $d = 2 ; 
            foreach($data_list as $dKey=>$dValue){
                $i = 0 ; 
                foreach($column_title as $key => $col){
                
                    if($col['intable']==1){

                        if($col['type']=='text'){
                            $objPHPExcel->getActiveSheet()->setCellValue($letter[$i].''.$d,$dValue[$col['key']]);
                        }if($col['type']=='img'){
                            $objPHPExcel->getActiveSheet()->setCellValue($letter[$i].''.$d,$dValue[$col['show_key']]);
                        }else if($col['type']=='number'){
                            $objPHPExcel->getActiveSheet()->setCellValue($letter[$i].''.$d,$dValue[$col['key']]);
                        }else if($col['type']=='enum'){
                            $objPHPExcel->getActiveSheet()->setCellValue($letter[$i].''.$d,$dValue[$col['show_key']]);
                        }else if($col['type']=='custom'){
                            if(isset($dValue[$col['key']])){
                                $objPHPExcel->getActiveSheet()->setCellValue($letter[$i].''.$d,$col['customMap'][$dValue[$col['key']]]);
                            }else{
                                $objPHPExcel->getActiveSheet()->setCellValue($letter[$i].''.$d,$dValue[$col['key']]);
                            }
                            
                        }else if($col['type']=='time'){
                            $tm = $dValue[$col['key']] ;
                            $stm = substr($tm,0,4).'-'.substr($tm,4,2).'-'.substr($tm,6,2).' '.substr($tm,8,2).':'.substr($tm,10,2).':'.substr($tm,12,2);
                            $objPHPExcel->getActiveSheet()->setCellValue($letter[$i].''.$d,$stm);
                        }
                        $i++;
                    }
                }
                $d++;
            }
        }
       

        // exit ;
       //  $objPHPExcel->getActiveSheet()->setCellValue('A1','hehe');
       // $objPHPExcel->getActiveSheet()->setCellValue('B1','hehe');
       // $objPHPExcel->getActiveSheet()->setCellValue('C1','hehe');
        
        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header('Content-Disposition:attachment;filename="'.$excel_name.'.xlsx"');
        header("Content-Transfer-Encoding:binary");
        $objWriter->save('php://output');

        // return $ret;

    }

  
    
    
}
